import json
from datetime import datetime

from wtforms import StringField, SelectField, HiddenField
from wtforms.validators import Optional, DataRequired, ValidationError

from apps.forms.base_form import BaseForm


class QueryAlbumEpisodeInfoForm(BaseForm):
    album_id = HiddenField('短剧ID')
    title = StringField('短剧标题', validators=[Optional()],
                        render_kw={"placeholder": "短剧标题", 'class': 'search-input'})


class CreateAlbumEpisodeInfoForm(BaseForm):
    album_id = HiddenField('短剧ID')
    title = StringField('剧集标题', validators=[DataRequired(message='请填写剧集标题')],
                        render_kw={"placeholder": "请填写剧集标题"})
    seq_index = StringField('剧集集数', validators=[DataRequired(message='请填写剧集集数')],
                          render_kw={"placeholder": "请填写剧集集数",
                                     "oninput": "this.value = this.value.replace(/[^0-9]/g, '');"})
    covers = HiddenField('封面图', validators=[DataRequired(message='请添加封面图')], default=[])
    video_library= HiddenField('视频素材', validators=[DataRequired(message='请添加视频素材')], default="")

    def validate_seq_index(self, field):
        if not self.is_integer(field.data):
            raise ValidationError('必须是一个大于0的整数')

    def validate_covers(self, field):
        try:
            covers_data = json.loads(field.data)
            if len(covers_data) < 1:
                raise ValidationError('请添加封面图')
        except Exception as e:
            raise ValidationError('请添加封面图')

    def validate_video_library(self, field):
        try:
            json_data = json.loads(field.data)
            if not json_data:
                raise ValidationError('请添加视频素材')
        except Exception as e:
            raise ValidationError('请添加视频素材')


class UpdateAlbumEpisodeInfoForm(BaseForm):
    album_episode_id = HiddenField('短剧剧集ID')
    album_id = HiddenField('短剧ID')
    title = StringField('剧集标题', validators=[DataRequired(message='请填写剧集标题')],
                        render_kw={"placeholder": "请填写剧集标题"})
    seq_index = StringField('剧集集数', validators=[DataRequired(message='请填写剧集集数')],
                          render_kw={"placeholder": "请填写剧集集数",
                                     "oninput": "this.value = this.value.replace(/[^0-9]/g, '');"})
    covers = HiddenField('封面图', validators=[DataRequired(message='请添加封面图')], default=[])
    video_library= HiddenField('视频素材', validators=[DataRequired(message='请添加视频素材')], default="")

    def __init__(self, model_dict=None, *args, **kwargs):
        super(UpdateAlbumEpisodeInfoForm, self).__init__(*args, **kwargs)
        if model_dict:
            self.album_episode_id.data = model_dict.get('id')
            self.title.data = model_dict.get('title')
            self.seq_index.data = str(model_dict.get('seq_index'))
            self.covers.data = model_dict.get('covers')
            self.video_library.data = model_dict.get('video_library')


    def validate_seq_index(self, field):
        if not self.is_integer(field.data):
            raise ValidationError('必须是一个大于0的整数')

    def validate_covers(self, field):
        try:
            covers_data = json.loads(field.data)
            if len(covers_data) < 1:
                raise ValidationError('请添加封面图')
        except Exception as e:
            raise ValidationError('请添加封面图')

    def validate_video_library(self, field):
        try:
            json_data = json.loads(field.data)
            if not json_data:
                raise ValidationError('请添加视频素材')
        except Exception as e:
            raise ValidationError('请添加视频素材')
